home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
pctj8404.arc
/
BINSRCH.ASM
next >
Wrap
Assembly Source File
|
1986-09-14
|
3KB
|
83 lines
;
ASM_SEG SEGMENT PARA PUBLIC
ASSUME CS:ASM_SEG
PUBLIC BINSEARCH
BINSEARCH PROC FAR
;
; THIS SUBROUTINE IS TO BE CALLED FROM BASIC OR
; ANOTHER HIGH-LEVEL PROGRAMMING LANGUAGE. IT
; DOES A BINARY SEARCH ON A DATA TABLE,
; RETURNING THE DESIRED INDEX INTO THE TABLE.
;
; FORMAT:
;
; CALL BINSEARCH (itable, ntable, jkey, indx)
;
; WHERE itable IS A TABLE OF INTEGER VALUES
; ntable IS THE NUMBER OF ITEMS IN THE TABLE
; jkey IS AN INTEGER WHOSE LOCATION IN THE TABLE
; IS DESIRED indx IS THE RESULT.
; jkey = itable(indx)
;
; NOTES:
; ITABLE MUST BE IN ASCENDING ORDER.
; JKEY MUST BE A VALID KEY, I.E., IT MUST BE
; CONTAINED IN ITABLE.
;
; SEE APPENDIX C. OF THE BASIC MANUAL FOR DISCUSSION
; OF THE BASIC CALLING SEQUENCE.
;
PUSH BP ;YES, THIS IS NECESSARY!
MOV BP, SP
;
MOV SI, [BP]+12 ;ADDRESS OF ITABLE
MOV DI, [BP]+10 ;ADDRESS OF NTABLE
MOV DX, [DI] ;NTABLE IN DX
MOV DI, [BP]+8 ;ADDRESS OF JKEY
MOV AX, [DI] ;JKEY IN AX
MOV DI, [BP]+6 ;ADDRESS OF INDX
MOV BP, SI ;MOVE ADDRESS OF ITABLE TO BP
;
; INITIALIZE ILOW, IHIGH
;
MOV CX, 1 ;CX=ILOW=1
; DX=IHIGH=NTABLE
SEARCH_LOOP:
;
MOV SI, DX ;IHIGH INTO SI
ADD SI, CX ;IHIGH+ILOW IN SI
SHR SI, 1 ;DIVIDE BY 2 (TRUNCATE): IMID
;
PUSH SI ;SAVE IMID, CALC. OFFSET NEXT
DEC SI ;ITABLE(1) IS OFFSET 0
SHL SI, 1 ;TWO BYTES PER ENTRY
;
CMP [BP][SI], AX ;COMP. ITABLE(IMID) WITH JKEY
;
POP SI ;RECOVER IMID
;
JE CLEAN_UP ;JKEY = ITABLE(IMID), FOUND IT
JG JKEY_SMALLER ;JKEY < ITABLE(IMID)
;
MOV CX, SI ;JKEY > ITABLE(IMID), NEW ILOW
INC CX ;ILOW = IMID + 1
JMP SEARCH_LOOP
JKEY_SMALLER:
MOV DX, SI ;NEW IHIGH
DEC DX ;IHIGH = IMID - 1
JMP SEARCH_LOOP
;
; END SEARCH_LOOP
;
CLEAN_UP:
MOV [DI], SI ;STORE INDX
;
POP BP ;RESTORE BP
RET 8 ;4 ARGUMENTS
;
BINSEARCH ENDP
;
ASM_SEG ENDS
;
END